import {
  HANDLE_CHANGE_INPUT,
  CHANGE_LIST,
  HANDLE_ENTER,
  HANDLE_LEAVE,
  HANDLE_CHANGE_PAGE
} from './constants'
import {
  fromJS
} from 'immutable'
const defaultState = fromJS({
  focused: false,
  mouseIn: false,
  list: [],
  page: 1,
  totalPage: 1
})
export default (state = defaultState, action) => {
  switch (action.type) {
    case HANDLE_CHANGE_INPUT:
      if (!state.get('focused')) {
        return state.set('focused', true).set('mouseIn', false)
      } else {
        return state.set('focused', false)
      }
    case CHANGE_LIST:
      return state.merge({
        list: action.data,
        totalPage: action.totalPage
      })
    case HANDLE_ENTER:
      return state.set('mouseIn', true)
    case HANDLE_LEAVE:
      return state.set('mouseIn', false)
    case HANDLE_CHANGE_PAGE:
      let page = state.get('page')
      const totalPage = state.get('totalPage')
      if (page < totalPage) {
        return state.set('page', ++page)
      } else {
        return state.set('page', 1)
      }
    default:
      return state
  }
}